<template>
	<view class="columnbox wdh-100 content">
		<!-- <view class="rowbox wdh-100 top-box">
			<view class="rowbox def-bgcol submit-btn" @click="submitInfo">保存</view>
		</view> -->
		<view v-if="keybordflag" style="width: 100%;height: 140rpx;"></view>
		<view class="yc-list">
			<view v-if="ycList.length>0" v-for="(item,index) in ycList" :key='index' class="one-yc columnbox spb">
				<view class="rowbox">
					<u-icon @click="deletYc(index)" name="minus-circle-fill" size="40rpx" color="#00b6b2"></u-icon>
					<view class="yc-name rowbox">{{item.chinesedrug}}
						<view v-if="item.price==0" style="color: #de101b;">（缺药）</view>
					</view>
				</view>
				<view class="rowbox" style="margin-top: 20rpx;">
					<u-number-box min="0" style="transform: scale(.9);" v-model="item.gram"
						@change=""></u-number-box><text style="font-size: 28rpx; color: #bcbcbc;">(克)</text>
				</view>
			</view>
			<view class="one-yc columnbox spb" style="height: 165rpx;" @click="showKeyboard">
				<view class="yc-name">请输入药材简拼</view>
				<view class="rowbox inputYaocai">点击输入</view>
				<!-- <input @focus="inputflag=true" class="yc-name" placeholder="请输入药材简拼" v-model="searchvalue" /> -->
			</view>
		</view>
		<!-- 输入 -->
		<keyboardBox :sotreId='sotreId' :jixingid='jixingid' @change='chooseYc' @cancel='keybordflag=false'
			ref="keyboard" :windowHeight='windowHeight'></keyboardBox>
		<view v-if="false" @click="inputflag=false" class="wdh-100 columnbox info-input"
			:style="{'height':windowHeight}">
			<!-- 键盘输入框 -->
			<view @click.stop="" class="one-yc rowbox spb" style="margin-left: 20rpx;">
				<view class="rowbox yc-name-input">{{searchvalueinput}} <text class="input-line">｜</text>
					<text v-if="searchvalueinput==''" style="color: #bcbcbc ;">请输入药材简拼</text>
				</view>
				<!-- <input @input="searchYaoCai" class="yc-name" placeholder="请输入药材简拼" v-model="searchvalue" /> -->
			</view>
			<view class="wdh-100 keybord-box columnbox" @click.stop="">
				<scroll-view scroll-x style="width: 100%;white-space: nowrap;">
					<view @click="chooseYc(item)" style="display: inline-block" v-for="(item,index) in searchResult"
						:key='index'>
						<view class="columnbox one-s-yc">
							<view>{{item.name}}</view>
							<view>{{item.price}}元/g</view>
						</view>
					</view>
				</scroll-view>
				<view class="columnbox key-bord wdh-100">
					<view class="rowbox wdh-100">
						<view @click="inputValue(item)" class="rowbox one-letter"
							v-for="(item,index) in keybord.slice(0, 10)" :key='index'>
							{{item}}
						</view>
					</view>
					<view class="rowbox wdh-100">
						<view @click="inputValue(item)" class="rowbox one-letter"
							v-for="(item,index) in keybord.slice(10, 19)" :key='index'>
							{{item}}
						</view>
					</view>
					<view class="rowbox wdh-100" style="position: relative;">
						<view @click="inputValue(item)" class="rowbox one-letter"
							v-for="(item,index) in keybord.slice(19, 26)" :key='index'>
							{{item}}
						</view>
						<view class="rowbox delet-icon" @click="deletLetter">
							<image src="/static/img/delet-icon.png" mode="widthFix"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="wdh-100" style="height: 200rpx;"></view>
		<view class="rowbox wdh-100 bottom-box" style="z-index: 2;">
			<view class="def-bgcol rowbox send-btn wdh-100" @click="submitInfo">保存修改</view>
		</view>
	</view>
</template>

<script>
	import keyboardBox from './components/keyboard'
	export default {
		components: {
			keyboardBox
		},
		data() {
			return {
				keybordflag: false,
				inputflag: false,
				ycList: [], //药材列表
				searchvalue: '',
				keybord: ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
					'Z', 'X', 'C', 'V', 'B', 'N', 'M'
				],
				searchvalueinput: '', //输入的搜索内容
				searchResult: [], //搜索结果
				windowHeight: '100vh',
				sotreId: 0,
				jixingid: 0
			};
		},
		onLoad(options) {
			if (options.info) {
				this.ycList = JSON.parse(options.info).Yaofangdrug
			}
			this.sotreId = options.sotreId * 1
			this.jixingid = options.jixingid * 1
			uni.getSystemInfo({
				success: (res) => {
					this.windowHeight = res.windowHeight + 'px'
				}
			})
		},
		methods: {
			// 显示键盘
			showKeyboard() {
				this.keybordflag = true
				this.$refs.keyboard.initData()
			},
			submitInfo() {
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例

				if (prevPage.$vm.chufang.Yaofangdrug) {
					prevPage.$vm.chufang.Yaofangdrug = this.ycList
				} else {
					prevPage.$vm.chufang = {
						...prevPage.$vm.chufang,
						Yaofangdrug: this.ycList
					}
				}
				let list = []
				this.ycList.forEach(v => {
					list.push(v.chinesedrug + ' ' + v.gram + 'g')
				})
				prevPage.$vm.chufang.cfList = list
				uni.navigateBack()
			},
			//删除药材
			deletYc(index) {
				this.ycList.splice(index, 1)
			},
			chooseYc(v) {
				this.keybordflag = false
				this.ycList.push(v)
				// this.ycList.push({
				// 	chinesedrug: v.name,
				// 	chinesedrugid: v.id,
				// 	gram: 1,
				// 	yaofangid: ''
				// })
				// console.log(v, this.ycList)
				// this.inputflag = false
				// this.searchvalueinput = ''
				// this.searchResult = []
			},
			//删除输入
			deletLetter() {
				if (this.searchvalueinput != '') {
					if (this.searchvalueinput.length == 1) {
						this.searchvalueinput = ''
						this.searchResult = []
					} else {
						this.searchvalueinput = this.searchvalueinput.substring(0, this.searchvalueinput.length - 1)
						this.$req.post('/doctor/chinesedrugs', {
							name: this.searchvalueinput
						}).then(res => {
							this.searchResult = res.data.data
						})
					}
				}
			},
			// 键盘输入
			inputValue(v) {
				if (this.searchvalueinput.length < 10) {
					this.searchvalueinput += v
					this.$req.post('/doctor/chinesedrugs', {
						name: this.searchvalueinput
					}).then(res => {
						this.searchResult = res.data.data
					})
				}
			},
			//搜索药材
			searchYaoCai(e) {
				if (e.detail.value != '' && e.detail.value.trim() != '') {
					this.$req.post('/doctor/chinesedrugs', {
						name: e.detail.value
					}).then(res => {

					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		min-height: 100vh;
		background: #F5F8FB;
		justify-content: flex-start;
		font-family: PingFang SC;
	}

	.yc-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		align-items: flex-start;
		padding-left: 20rpx;
		box-sizing: border-box;
		width: 100%;
	}


	.one-yc {
		margin-right: 20rpx;
		background: #fff;
		padding: 20rpx 20rpx;
		border-radius: 10rpx;
		margin-top: 20rpx;
		width: calc((100% - 40rpx)/2);
		align-items: flex-start;
	}

	.yc-name,
	.yc-name-input {
		font-weight: bold;
		font-size: 26rpx;
		color: #0E0E0E;
		margin-left: 20rpx;
	}

	.yc-name-input {
		font-size: 32rpx;
		font-weight: normal;
	}

	.yc-weight {
		font-weight: 500;
		font-size: 22rpx;
		color: #939BA7;
	}

	/deep/.uni-input-input {
		font-size: 24rpx;
	}

	.info-input {
		background: rgba(#000, 0.7);
		position: fixed;
		top: var(--window-top);
		left: 0;
		z-index: 9;
		justify-content: space-between;
		align-items: flex-start;
		box-sizing: border-box;
	}

	.keybord-box {
		width: 100%;
		background: #fff;
		padding-bottom: 0;

		.one-s-yc {
			padding: 0 20rpx;
			margin: 20rpx 0;
		}

		.one-s-yc>view:nth-child(1) {
			font-size: 28rpx;
			font-weight: bold;
		}

		.one-s-yc>view:nth-child(2) {
			font-size: 24rpx;
			color: #4c4d4d;
			margin-top: 10rpx;
		}

		.key-bord {
			background: #ededed;
			padding: 20rpx 0;
			padding-bottom: 70rpx;

			.one-letter {
				background: #ffffff;
				color: #5b5b5b;
				width: 56rpx;
				height: 70rpx;
				border-radius: 10rpx;
				margin: 10rpx 10rpx;
			}

			.delet-icon {
				position: absolute;
				right: 32rpx;
				background: #ffffff;
				color: #5b5b5b;
				width: 82rpx;
				height: 70rpx;
				border-radius: 10rpx;

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}
		}
	}

	.input-line {
		color: #bab7bb;
		animation: dropdown 1s linear infinite;
		font-size: 40rpx;
	}

	@keyframes dropdown {
		0% {
			display: none;
			opacity: 0;
		}

		50% {
			display: flex;
			opacity: 1;
		}
	}

	.yc-noinfo {
		font-size: 32rpx;
	}

	.top-box {
		background: #fff;
		justify-content: flex-end;
		padding-bottom: 20rpx;
		position: fixed;
		top: var(--window-top);
		left: 0;
		z-index: 9;
	}

	.submit-btn {
		font-weight: bold;
		margin-top: 20rpx;
		font-size: 32rpx;
		margin-right: 20rpx;
		color: #fff;
		height: 62rpx;
		width: 120rpx;
		border-radius: 10rpx;
	}

	.inputYaocai {
		width: 90%;
		margin-left: 20rpx;
		background: #eff3f6;
		font-size: 28rpx;
		border-radius: 4rpx;
		justify-content: flex-start;
		padding: 10rpx 20rpx;
		color: #767d57;
	}
</style>